﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;

public partial class leftframe : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand comm;
    private Int64 userID = 0;
    public string userCode;
    public string userName;
    public StringBuilder BusinessFunction = new StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userID"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            conn = DBTools.getDBConnection();
            comm = new SqlCommand("", conn);
            userID = Int64.Parse(Session["userID"].ToString());
            StringBuilder sql = new StringBuilder();
            sql.Append(" select"                                                                + Environment.NewLine); 
            sql.Append(" A3.ID,"                                                                + Environment.NewLine);
            sql.Append(" A3.Name,"                                                              + Environment.NewLine);
            sql.Append(" A4.ID,"                                                                + Environment.NewLine);
            sql.Append(" A4.Name"                                                               + Environment.NewLine);
            sql.Append(" from"                                                                  + Environment.NewLine);
            sql.Append(" SH_YXK_User as A"                                                      + Environment.NewLine);
            sql.Append(" join SH_YXK_UserFunctionRelation as A1 on A1.Users = A.ID"             + Environment.NewLine);
            sql.Append(" join SH_YXK_BusinessFunction as A2 on A2.ID = A1.BusinessFunction"     + Environment.NewLine);
            sql.Append(" join SH_YXK_BusinessClass as A3 on A3.ID = A2.BusinessClass"           + Environment.NewLine);
            sql.Append(" join SH_YXK_BusinessModule as A4 on A4.ID = A3.BusinessModule"         + Environment.NewLine);
            sql.Append(" where A.ID = " + userID + " "                                          + Environment.NewLine);
            sql.Append(" group by "                                                             + Environment.NewLine);
            sql.Append(" A4.ID,"                                                                + Environment.NewLine);
            sql.Append(" A4.Name,"                                                              + Environment.NewLine);
            sql.Append(" A3.ID,"                                                                + Environment.NewLine);
            sql.Append(" A3.Name"                                                               + Environment.NewLine);
            comm.CommandText = sql.ToString();
            conn.Open();
            SqlDataReader sdr1 = comm.ExecuteReader();
            BusinessFunction.Append("var outlookbar=new outlook();" + Environment.NewLine);
            BusinessFunction.Append("var t;" + Environment.NewLine);
            SqlConnection conn1 = DBTools.getDBConnection();
            SqlCommand comm1 = new SqlCommand("", conn1);
            while (sdr1.Read())
            {
                BusinessFunction.Append("t = outlookbar.addtitle(" + sdr1[0] + ", '" + sdr1[1] + "', " + sdr1[2] + ", '" + sdr1[3] + "', 1);");
                StringBuilder sql1 = new StringBuilder();
                sql1.Append(" select "                                                                  + Environment.NewLine);
                sql1.Append(" A2.ID,"                                                                   + Environment.NewLine);
                sql1.Append(" A2.Name,"                                                                 + Environment.NewLine);
                sql1.Append(" A2.URL"                                                                   + Environment.NewLine);
                sql1.Append(" from"                                                                     + Environment.NewLine);
                sql1.Append(" SH_YXK_User as A"                                                         + Environment.NewLine);
                sql1.Append(" join SH_YXK_UserFunctionRelation as A1 on A1.Users = A.ID"                + Environment.NewLine);
                sql1.Append(" join SH_YXK_BusinessFunction as A2 on A2.ID = A1.BusinessFunction"        + Environment.NewLine);
                sql1.Append(" join SH_YXK_BusinessClass as A3 on A3.ID = A2.BusinessClass"              + Environment.NewLine);
                sql1.Append(" join SH_YXK_BusinessModule as A4 on A4.ID = A3.BusinessModule"            + Environment.NewLine);
                sql1.Append(" where"                                                                    + Environment.NewLine);
                sql1.Append(" A.ID = " + userID + "  --用户id"                                          + Environment.NewLine);
                sql1.Append(" and A3.ID = " + sdr1[0] + "--分类id"                                      + Environment.NewLine);
                
                comm1.CommandText = sql1.ToString();
                conn1.Open();
                SqlDataReader sdr2 = comm1.ExecuteReader();
                while (sdr2.Read())
                {
                    BusinessFunction.Append("outlookbar.additem(" + sdr2[0]+",'" + sdr2[1]+"',t,'" + sdr2[2] + "');");
                }
                sdr2.Close();
                conn1.Close();
            }
            sdr1.Close();
            sdr1.Dispose();
            conn.Close();

            string sql2 = "select Code,Name from SH_YXK_User where ID =" + userID;
            comm.CommandText = sql2;
            conn.Open();
            sdr1 = comm.ExecuteReader();
            while (sdr1.Read())
            {
                userCode = sdr1[0].ToString();
                userName = sdr1[1].ToString();
            }
            sdr1.Close();
            sdr1.Dispose();
            conn.Close();
        }
    }
}